package cn.changeforyou.analyzer.dbParse.model;

import cn.changeforyou.analyzer.CombineElement;
import cn.changeforyou.analyzer.combine.orm.compare.ComparedStruct;
import cn.changeforyou.analyzer.naming.NamingStrategy;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * @author zhyu
 * @date 2021/2/8 16:44
 */
@Data
public class MysqlTableInfo implements ComparedStruct {

    private String tableName;

    private List<ColumnInfo> columnInfos;
    
    private String tableComment;
    
    @Override
    public List<String> getComparedStruct() {
        List<String> names = columnInfos.stream().map(x -> x.getName(getNamingStrategy())).collect(Collectors.toList());
        Collections.sort(names);
        return names;
    }
    
    public NamingStrategy getNamingStrategy() {
        return NamingStrategy.underline;
    }
    
    public String getStructName() {
        return tableName;
    }
    
    @Getter@Setter
    public static class ColumnInfo implements CombineElement {

        private String columnName;

        private String columnType;

        private String columnComment;
        
        private MysqlTableInfo mysqlTableInfo;
	
	    public ColumnInfo(MysqlTableInfo mysqlTableInfo) {
		    this.mysqlTableInfo = mysqlTableInfo;
	    }
	
	    @Override
	    public String getName() {
		    return columnName;
	    }
	
	    @Override
	    public ComparedStruct getParentStruct() {
		    return mysqlTableInfo;
	    }
	
	    @Override
	    public String toString() {
		    return "ColumnInfo{" +
			           "columnName='" + columnName + '\'' +
			           ", columnType='" + columnType + '\'' +
			           ", columnComment='" + columnComment + '\'' +
			           ", mysqlTableInfo=@" + mysqlTableInfo.hashCode() +
			           '}';
	    }
    }
}
